rm(list=ls(all=TRUE))
library(metafor)
library(clubSandwich)
library(dplyr)
library(ggplot2)

setwd("~/Dropbox/Apps/ShareLaTeX/Minimal Effects/replication/data")

data <- read.csv("MPSA Persuasion MetaAnalysis - Candidate Campaign.csv", stringsAsFactors = FALSE)

# Recoding fun
data <- rename(data, CACE = CACE..in.pp., SE = SE..in.pp.)
data$CACE <- data$CACE * 100
data$SE <- data$SE * 100

data$variance <- data$SE^2
data$precision <- 1/data$variance

data <- subset(data, Experiment...for.clustering != 'Spenkuch and Toniatti (2016)') # Not really natural experiment or DID
data <- subset(data, Experiment...for.clustering != 'Huber and Arceneaux (2007)') # Not really natural experiment or DID
data <- subset(data, Experiment...for.clustering != 'Shaw and Gimpel (2012)') # Not really outreach from a campaign
data <- subset(data, substr(data$Experiment...for.clustering, 1, 5) != 'Green') # Not really outreach from a campaign

data$Experiment <- factor(data$Experiment...for.clustering)

# General vs. Primary
data$General <- ifelse(data$Primary.or.General == "General", 1, 0)
data$Primary <- ifelse(data$Primary.or.General == "General", 0, 1)

Governor <- rma.mv(yi = CACE, V = variance, random = list(~1 | Experiment), data = subset(data, data$General == 1 & data$Seat == "Governor"))[1:2]
President <- rma.mv(yi = CACE, V = variance, random = list(~1 | Experiment), data = subset(data, data$General == 1 & data$Seat == "President"))[1:2]
Congress <- rma.mv(yi = CACE, V = variance, random = list(~1 | Experiment), data = subset(data, data$General == 1 & (data$Seat == "US House" | data$Seat == "US Senate")))[1:2]
Local <- rma.mv(yi = CACE, V = variance, random = list(~1 | Experiment), data = subset(data, data$General == 1 & 
                                                                                         (data$Seat == "County" | data$Seat == "Mayoral" | data$Seat == "Magistrate")))[1:2]
StateLeg <- rma.mv(yi = CACE, V = variance, random = list(~1 | Experiment), data = subset(data, data$General == 1 & data$Seat == "State Legislative"))[1:2]

summary.meta <- data.frame(rbind(President, Governor, Congress, StateLeg, Local))
summary.meta$Subset <- rownames(summary.meta)

summary.meta$b <- as.numeric(as.character(summary.meta$b))
summary.meta$se <- as.numeric(as.character(summary.meta$se))
summary.meta$Subset <- factor(summary.meta$Subset, levels=rev(unique(as.character(summary.meta$Subset))))

#Make plot
pdf('../figures/summary_electiontype.pdf', width = 14, height = 10)
ggplot(summary.meta, aes(x=Subset, y=b)) +
  geom_point(size = 5) + 
  geom_errorbar(width=.1, aes(ymin=b - 1.96 * se, ymax=b + 1.96 * se), colour = "blue") + 
  xlab("") + ylab("Random Effects Estimate, General Elections") + 
  scale_y_continuous(
    "Random Effects Estimate, General Elections", 
    sec.axis = sec_axis(~ ., name = "Random Effects Estimate, General Elections")) + 
  geom_hline(yintercept = 0, colour = "red") + 
  theme_bw() + 
  theme(axis.text=element_text(size=22),
        axis.title=element_text(size=20)) +
  coord_flip()
dev.off()

#What is the local effect without Barton et al. (2014)?
rma.mv(yi = CACE, V = variance, random = list(~1 | Experiment), data = subset(data, data$General == 1 & data$Treatment.Mode != "Candidate Canvass" &
                                                                                (data$Seat == "County" | data$Seat == "Mayoral" | data$Seat == "Magistrate")))
